package com.ruoyi.shop.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.shop.util.pay.PayStatus;
import com.ruoyi.shop.util.pay.UserOrderConstants;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import org.apache.commons.lang3.time.DateUtils;

/**
 * 订单对象 shop_user_order
 * 
 * @author Alan Zhang
 * @date 2021-09-14
 */
public class ShopUserOrder extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 订单id */
    private Long id;

    /** 订单号 */
    @Excel(name = "订单号")
    private String ns;

    /** 商品的类型 */
    @Excel(name = "商品的类型")
    private String productType;

    /** 商品的名称 */
    @Excel(name = "商品的名称")
    private String productTitle;

    /** 商品描述 */
    @Excel(name = "商品描述")
    private String productSummary;

    /** 购买人 */
    @Excel(name = "购买人")
    private Long buyerId;

    /** 购买人昵称 */
    @Excel(name = "购买人昵称")
    private String buyerNickname;

    /** 用户留言 */
    @Excel(name = "用户留言")
    private String buyerMsg;

    /** 订单总金额，购买人员应该付款的金额 */
    @Excel(name = "订单总金额")
    private BigDecimal orderTotalAmount;

    /** 订单的真实金额，销售人员可以在后台修改支付金额，一般情况下 order_real_amount = order_total_amount */
    @Excel(name = "订单的真实金额")
    private BigDecimal orderRealAmount;

    /** 优惠码 */
    @Excel(name = "优惠码")
    private String couponCode;

    /** 优惠金额 */
    @Excel(name = "优惠金额")
    private BigDecimal couponAmount;

    /** 支付状态：1未付款、 2用户标识已经线下付款完成、3用户标识已经通过微信或者支付宝等工具支付完成 、9已经付款（线上支付）、10已经下线支付、11已经通过微信或支付宝等工具支付 */
    @Excel(name = "支付状态")
    private Integer payStatus;

    /** 支付成功的金额 */
    @Excel(name = "支付成功的金额")
    private BigDecimal paySuccessAmount;

    /** 支付时间 */
    @JsonFormat(pattern = "yyyy-MM-dd")
    @Excel(name = "支付时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date paySuccessTime;

    /** 支付证明，手动入账时需要截图 */
    @Excel(name = "支付证明")
    private String paySuccessProof;

    /** 支付备注 */
    @Excel(name = "支付备注")
    private String paySuccessRemarks;

    /** 支付记录 */
    @Excel(name = "支付记录")
    private Long paymentId;

    /** 第三方订单号 */
    @Excel(name = "第三方订单号")
    private String paymentOuterId;

    /** 第三方支付用户，一般情况下是用户的openId */
    @Excel(name = "第三方支付用户")
    private String paymentOuterUser;

    /** 发货情况 */
    @Excel(name = "发货情况")
    private Long deliveryId;

    /** 发货类型 1 邮寄 2 自取 3配送*/
    @Excel(name = "发货类型")
    private Integer deliveryType;

    /** 发货状态 1未发货  2已发货  3已送达*/
    @Excel(name = "发货状态")
    private Integer deliveryStatus;

    /** 收货人地址 */
    @Excel(name = "收货人地址")
    private String deliveryAddrUsername;

    /** 收货人手机号（电话） */
    @Excel(name = "收货人手机号")
    private String deliveryAddrMobile;

    /** 收件人省 */
    @Excel(name = "省")
    private String deliveryAddrProvince;

    /** 收件人的城市 */
    @Excel(name = "城市")
    private String deliveryAddrCity;

    /** 收件人的区（县） */
    @Excel(name = "区")
    private String deliveryAddrDistrict;

    /** 收件人的详细地址 */
    @Excel(name = "详细地址")
    private String deliveryAddrDetail;

    /** 收件人地址邮政编码 */
    @Excel(name = "邮政编码")
    private String deliveryAddrZipcode;

    /** 交易状态：1交易中、 2交易完成（但是可以申请退款） 、3取消交易 、4申请退款、 5拒绝退款、 6退款中、 7退款完成、 9交易结束 */
    @Excel(name = "交易状态")
    private Integer tradeStatus;

    /** 删除状态：1 正常 ，2 回收站 3 已经删除 */
    private Integer delFlag;

    private List<ShopUserOrderItem> shopUserOrderItemList;

    public void setId(Long id) 
    {
        this.id = id;
    }

    public Long getId() 
    {
        return id;
    }
    public void setNs(String ns) 
    {
        this.ns = ns;
    }

    public String getNs() 
    {
        return ns;
    }
    public void setProductType(String productType) 
    {
        this.productType = productType;
    }

    public String getProductType() 
    {
        return productType;
    }
    public void setProductTitle(String productTitle) 
    {
        this.productTitle = productTitle;
    }

    public String getProductTitle() 
    {
        return productTitle;
    }
    public void setProductSummary(String productSummary) 
    {
        this.productSummary = productSummary;
    }

    public String getProductSummary() 
    {
        return productSummary;
    }
    public void setBuyerId(Long buyerId) 
    {
        this.buyerId = buyerId;
    }

    public Long getBuyerId() 
    {
        return buyerId;
    }
    public void setBuyerNickname(String buyerNickname) 
    {
        this.buyerNickname = buyerNickname;
    }

    public String getBuyerNickname() 
    {
        return buyerNickname;
    }
    public void setBuyerMsg(String buyerMsg) 
    {
        this.buyerMsg = buyerMsg;
    }

    public String getBuyerMsg() 
    {
        return buyerMsg;
    }
    public void setOrderTotalAmount(BigDecimal orderTotalAmount) 
    {
        this.orderTotalAmount = orderTotalAmount;
    }

    public BigDecimal getOrderTotalAmount() 
    {
        return orderTotalAmount;
    }
    public void setOrderRealAmount(BigDecimal orderRealAmount) 
    {
        this.orderRealAmount = orderRealAmount;
    }

    public BigDecimal getOrderRealAmount() 
    {
        return orderRealAmount;
    }
    public void setCouponCode(String couponCode) 
    {
        this.couponCode = couponCode;
    }

    public String getCouponCode() 
    {
        return couponCode;
    }
    public void setCouponAmount(BigDecimal couponAmount) 
    {
        this.couponAmount = couponAmount;
    }

    public BigDecimal getCouponAmount() 
    {
        return couponAmount;
    }
    public void setPayStatus(Integer payStatus) 
    {
        this.payStatus = payStatus;
    }

    public Integer getPayStatus() 
    {
        return payStatus;
    }
    public void setPaySuccessAmount(BigDecimal paySuccessAmount) 
    {
        this.paySuccessAmount = paySuccessAmount;
    }

    public BigDecimal getPaySuccessAmount() 
    {
        return paySuccessAmount;
    }
    public void setPaySuccessTime(Date paySuccessTime) 
    {
        this.paySuccessTime = paySuccessTime;
    }

    public Date getPaySuccessTime() 
    {
        return paySuccessTime;
    }
    public void setPaySuccessProof(String paySuccessProof) 
    {
        this.paySuccessProof = paySuccessProof;
    }

    public String getPaySuccessProof() 
    {
        return paySuccessProof;
    }
    public void setPaySuccessRemarks(String paySuccessRemarks) 
    {
        this.paySuccessRemarks = paySuccessRemarks;
    }

    public String getPaySuccessRemarks() 
    {
        return paySuccessRemarks;
    }
    public void setPaymentId(Long paymentId) 
    {
        this.paymentId = paymentId;
    }

    public Long getPaymentId() 
    {
        return paymentId;
    }
    public void setPaymentOuterId(String paymentOuterId) 
    {
        this.paymentOuterId = paymentOuterId;
    }

    public String getPaymentOuterId() 
    {
        return paymentOuterId;
    }
    public void setPaymentOuterUser(String paymentOuterUser) 
    {
        this.paymentOuterUser = paymentOuterUser;
    }

    public String getPaymentOuterUser() 
    {
        return paymentOuterUser;
    }
    public void setDeliveryId(Long deliveryId)
    {
        this.deliveryId = deliveryId;
    }

    public Long getDeliveryId()
    {
        return deliveryId;
    }
    public void setDeliveryType(Integer deliveryType)
    {
        this.deliveryType = deliveryType;
    }

    public Integer getDeliveryType()
    {
        return deliveryType;
    }
    public void setDeliveryStatus(Integer deliveryStatus)
    {
        this.deliveryStatus = deliveryStatus;
    }

    public Integer getDeliveryStatus()
    {
        return deliveryStatus;
    }
    public void setDeliveryAddrUsername(String deliveryAddrUsername) 
    {
        this.deliveryAddrUsername = deliveryAddrUsername;
    }

    public String getDeliveryAddrUsername() 
    {
        return deliveryAddrUsername;
    }
    public void setDeliveryAddrMobile(String deliveryAddrMobile) 
    {
        this.deliveryAddrMobile = deliveryAddrMobile;
    }

    public String getDeliveryAddrMobile() 
    {
        return deliveryAddrMobile;
    }
    public void setDeliveryAddrProvince(String deliveryAddrProvince) 
    {
        this.deliveryAddrProvince = deliveryAddrProvince;
    }

    public String getDeliveryAddrProvince() 
    {
        return deliveryAddrProvince;
    }
    public void setDeliveryAddrCity(String deliveryAddrCity) 
    {
        this.deliveryAddrCity = deliveryAddrCity;
    }

    public String getDeliveryAddrCity() 
    {
        return deliveryAddrCity;
    }
    public void setDeliveryAddrDistrict(String deliveryAddrDistrict) 
    {
        this.deliveryAddrDistrict = deliveryAddrDistrict;
    }

    public String getDeliveryAddrDistrict() 
    {
        return deliveryAddrDistrict;
    }
    public void setDeliveryAddrDetail(String deliveryAddrDetail) 
    {
        this.deliveryAddrDetail = deliveryAddrDetail;
    }

    public String getDeliveryAddrDetail() 
    {
        return deliveryAddrDetail;
    }
    public void setDeliveryAddrZipcode(String deliveryAddrZipcode) 
    {
        this.deliveryAddrZipcode = deliveryAddrZipcode;
    }

    public String getDeliveryAddrZipcode() 
    {
        return deliveryAddrZipcode;
    }
    public void setTradeStatus(Integer tradeStatus) 
    {
        this.tradeStatus = tradeStatus;
    }

    public Integer getTradeStatus() 
    {
        return tradeStatus;
    }
    public void setDelFlag(Integer delFlag) 
    {
        this.delFlag = delFlag;
    }

    public Integer getDelFlag() 
    {
        return delFlag;
    }

    public List<ShopUserOrderItem> getShopUserOrderItemList() {
        return shopUserOrderItemList;
    }

    public void setShopUserOrderItemList(List<ShopUserOrderItem> shopUserOrderItemList) {
        this.shopUserOrderItemList = shopUserOrderItemList;
    }

    public boolean isDeliveried() {
        Integer status = getDeliveryStatus();
        return status != null && (status == UserOrderConstants.DELIVERY_STATUS_DELIVERIED || status == UserOrderConstants.DELIVERY_STATUS_FINISHED || status == UserOrderConstants.DELIVERY_STATUS_NONEED);
    }

    public boolean isNotDeliveried() {
        Integer status = getDeliveryStatus();
        return status != null && (status == UserOrderConstants.DELIVERY_STATUS_UNDELIVERY || status == UserOrderConstants.DELIVERY_STATUS_NEED_RE_DELIVERY);
    }

    public boolean isDeliverFinished() {
        Integer status = getDeliveryStatus();
        return status != null && (status == UserOrderConstants.DELIVERY_STATUS_FINISHED || status == UserOrderConstants.DELIVERY_STATUS_NONEED);
    }


    public boolean isUnpay() {
        Integer payStatus = getPayStatus();
        return payStatus != null && payStatus == PayStatus.UNPAY.getStatus();
    }

    public boolean isPaySuccess() {
        Integer payStatus = getPayStatus();
        return payStatus != null && payStatus >= PayStatus.SUCCESS_ALIPAY.getStatus();
    }

    public boolean isFinished() {
        Integer tradeStatus = getTradeStatus();
        return tradeStatus != null && UserOrderConstants.TRADE_STATUS_FINISHED == tradeStatus;
    }

    /**
     * 订单是否已经关闭
     *
     * @return
     */
    public boolean isClosed() {
        Integer tradeStatus = getTradeStatus();
        if (tradeStatus != null && UserOrderConstants.TRADE_STATUS_CLOSED == tradeStatus) {
            return true;
        }

        //支付成功的订单不关闭
        if (isPaySuccess()) {
            return false;
        }

        if(getCreateTime() == null){
            return false;
        }
        int orderTimeout = 1;
        return DateUtils.addDays(getCreateTime(), orderTimeout).before(new Date());
    }


    @Override
    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("id", getId())
            .append("ns", getNs())
            .append("productType", getProductType())
            .append("productTitle", getProductTitle())
            .append("productSummary", getProductSummary())
            .append("buyerId", getBuyerId())
            .append("buyerNickname", getBuyerNickname())
            .append("buyerMsg", getBuyerMsg())
            .append("orderTotalAmount", getOrderTotalAmount())
            .append("orderRealAmount", getOrderRealAmount())
            .append("couponCode", getCouponCode())
            .append("couponAmount", getCouponAmount())
            .append("payStatus", getPayStatus())
            .append("paySuccessAmount", getPaySuccessAmount())
            .append("paySuccessTime", getPaySuccessTime())
            .append("paySuccessProof", getPaySuccessProof())
            .append("paySuccessRemarks", getPaySuccessRemarks())
            .append("paymentId", getPaymentId())
            .append("paymentOuterId", getPaymentOuterId())
            .append("paymentOuterUser", getPaymentOuterUser())
            .append("deliveryId", getDeliveryId())
            .append("deliveryType", getDeliveryType())
            .append("deliveryStatus", getDeliveryStatus())
            .append("deliveryAddrUsername", getDeliveryAddrUsername())
            .append("deliveryAddrMobile", getDeliveryAddrMobile())
            .append("deliveryAddrProvince", getDeliveryAddrProvince())
            .append("deliveryAddrCity", getDeliveryAddrCity())
            .append("deliveryAddrDistrict", getDeliveryAddrDistrict())
            .append("deliveryAddrDetail", getDeliveryAddrDetail())
            .append("deliveryAddrZipcode", getDeliveryAddrZipcode())
            .append("tradeStatus", getTradeStatus())
            .append("delFlag", getDelFlag())
            .append("updateTime", getUpdateTime())
            .append("createTime", getCreateTime())
            .toString();
    }
}
